package com.af.security.mgt.user;

import com.github.yitter.idgen.YitIdHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

@Service
public class UserService {

    @Autowired
    private UserRepository userRepository;

    public Boolean existsByUsername(String userName) {
        return userRepository.existsByUsername(userName);
    }
    public Boolean existsByEmail(String email) {
        return userRepository.existsByEmail(email);
    }
    public Boolean existsByPhone(String phone) {
        return userRepository.existsByPhone(phone);
    }

    public User findByUsername(String userName) {
        return userRepository.findByUsername(userName);
    }
    public User findBySourceId(String sourceId) {
        return userRepository.findBySourceId(sourceId);
    }
    public User findByEmail(String email) {
        return userRepository.findByEmail(email);
    }
    public User findByPhone(String phone) {
        return userRepository.findByPhone(phone);
    }
    public User findByUsernameOrEmailOrPhone(String name) {
        return userRepository.findByUsernameOrEmailOrPhone(name,name,name);
    }

    @Transactional
    public User save(User user) {
        if (user.getId() != null){
            user.setId(YitIdHelper.nextId());
        }
        return userRepository.save(user);
    }

    @Transactional
    public void update(User user) {
        userRepository.update(user.getUsername(), user.getEmail(), user.getPhone(), user.getId());
    }

    @Transactional
    public void deleteById(Long id) {
        userRepository.deleteById(id);
    }

    @Transactional
    public void deleteByIds(String[] ids) {
        for (String id : ids) {
            userRepository.deleteById(Long.valueOf(id));
        }
    }

    @Transactional(readOnly = true)
    public Page<User> findByUsername(String username, Integer pi, Integer ps) {
        Sort sort = Sort.by(Sort.Direction.DESC, "id");
        Pageable pageable = PageRequest.of(pi, ps, sort);
        if (StringUtils.isEmpty(username)) {
            return userRepository.findAll(pageable);
        } else {
            return userRepository.findByUsername(username, pageable);
        }
    }


    @Transactional
    public void saveUserRole(Long userId, String roleIdsStr) {
        userRepository.deleteUserRoleByUserId(userId);
        String[] ids = roleIdsStr.split(",");
        for (String id : ids) {
            if (StringUtils.isEmpty(id)) {
                continue;
            }
            Long roleId = Long.valueOf(id);
            userRepository.InsertUserRoleByUserId(userId, roleId);
        }
    }

    @Transactional(readOnly = true)
    public User getById(Long id) {
        return userRepository.getOne(id);
    }

    @Transactional(rollbackFor = Exception.class)
    public User userLocked(String username, Boolean notLocked) {
        User user = findByUsername(username);
        if (user != null) {
            user.setAccountNonLocked(notLocked);
            return save(user);
        }
        return null;
    }
}